package com.web.exiaodao.remote.controller;

import javax.servlet.http.HttpServletRequest;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.util.ReflectionUtils;
import com.web.webstart.base.util.XaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.result.XaResult;
import com.web.exiaodao.remote.service.ApiFeedBackService;
import com.web.exiaodao.remote.vo.FeedBackVo;
import com.web.exiaodao.business.entity.FeedBack;

/**
 * @author eason.zt
 * @version V1.0
 * @Title: ApiFeedBackController.java
 * @Package com.web.exiaodao.remote.controller
 * @Description: 意见反馈信息接口
 * @date 2015年3月23日 下午1:00:00
 */
@Api(value = "FeedBack", description = "意见反馈接口", position = 10)
@Controller
@RequestMapping("/api/feedBack")
public class ApiFeedBackController extends BaseController {

    @Autowired
    private ApiFeedBackService feedBackService;

    /**
     * @return
     * @Title: save
     * @Description: 新增一条实体
     * //@param userId
     * //@param model
     */
    @ApiOperation(value = "新增意见反馈", notes = "新增意见反馈,当返回的code=1时，保存成功后object返回对象")
    @ResponseBody
    @RequestMapping(value = "saveFeedBack", method = RequestMethod.POST)
    public XaResult<FeedBackVo> saveFeedBack(
            @ApiParam("用户Id,字段名:userId") @RequestParam(value = "userId", required = false) Long userId,
            @ApiParam("反馈内容,字段名:content") @RequestParam(value = "content") String content,
            @ApiParam("反馈图片,字段名:url") @RequestParam(value = "url", required = false) String url,
            HttpServletRequest request
    ) throws BusinessException {
        XaResult<FeedBackVo> xr = new XaResult<FeedBackVo>();
        try {
            if (XaUtil.isEmpty(content)) {
                xr.error("反馈内容不能为空");
                return xr;
            }
            int length = ReflectionUtils.getHibernateColumnAnnotationLength(FeedBack.class, "content");
            if (content.length() > length) {
                xr.error("反馈内容不能超过" + length);
                return xr;
            }
            if (XaUtil.isNotEmpty(url)) {
                if (url.indexOf(",") == 0 || url.indexOf(",") == (url.length() - 1) || url.length() <= 5) {
                    xr.error("图片格式不正确");
                    return xr;
                }
            }
            FeedBack model = new FeedBack();
            model.setUserId(userId);
            model.setContent(content);
            model.setUrl(url);

            xr = feedBackService.createModel(model);
        } catch (Exception e) {
            if (e instanceof BusinessException) {
                xr.error(e.getMessage());
            } else {
                xr.error(XaConstant.Message.error);
            }
        }
        return xr;
    }

}

